<?php

namespace catchAdmin\pc\model;

use catchAdmin\model\Notice;
use catchAdmin\model\UserCompanyStatusLog;
use catchAdmin\model\WechatUser;
use catchAdmin\permissions\model\Users;
use catcher\exceptions\AlertErrorException;

class UserCompany extends \catchAdmin\model\UserCompany
{


    public $name = 'user_company';


    public function indexData($param)
    {
        $app = $this
            ->with(['wechatUser']);

        return $this->indexDataPcCC($param, $app);
    }


    public function indexByNeed($param)
    {
        $app = $this;

        $sql = "id in (";
        $sql .= " select user_company_id from need_match_user";
        $sql .= " where pay_time > 0";
        $sql .= " and need_id = " . $param['need_id'];
        $sql .= " )";

        $app = $app
            ->where($sql);

        return $this->indexDataPcCC($param, $app);
    }


    /**
     * 审核日志
     */
    public function auditLog($param)
    {
        $app = (new UserCompanyStatusLog())
            ->whereIn('company_status', [3, 4])
            ->order('id', 'desc');

        $param['limit'] = 999;

        return (new UserCompanyStatusLog())->indexDataPcCC($param, $app)['data'];
    }


    /**
     * 审核
     */
    public function audit($param)
    {
        $user_company_msg = $this
            ->where('id', $param['id'])
            ->find();
        if ($user_company_msg->company_status != 2) {
            throw new AlertErrorException('不能重复审核');
        }

        $this->startTrans();

        // 1.更新公司表，并记日志
        {
            $user_company_msg = $this->saveDataCC([
                'id'                  => $user_company_msg['id'],
                'audit_time'          => time(),
                'company_status'      => $param['company_status'],
                'company_status_note' => $param['company_status_note'] ?? '',
            ]);
            (new UserCompanyStatusLog())->saveDataCC([
                'user_id'             => $param['creator_id'],
                'wechat_user_id'      => $user_company_msg['wechat_user_id'],
                'user_company_id'     => $user_company_msg['id'],
                'status_no'           => $user_company_msg['status_no'],
                'company_status'      => $user_company_msg['company_status'],
                'company_status_note' => $user_company_msg['company_status_note'],
            ]);
        }

        // 2.更新用户表审核状态
        {
            $wechat_user_msg = (new \catchAdmin\model\WechatUser())->saveDataCC([
                'id'             => $user_company_msg['wechat_user_id'],
                'company_status' => $user_company_msg['company_status'],
                'company_name'   => $user_company_msg['company_name'],
            ]);

            if ($user_company_msg['company_status'] == 3) {
                $share_code = $wechat_user_msg->share_code;
                if (empty($share_code)) {
                    $share_code = (new WechatUser())->getShareCode();
                    $wechat_user_msg->share_code = $share_code;
                }
                $wechat_user_msg->share_img = (new \catchAdmin\model\WechatUser())->getShareImg($wechat_user_msg->id, $share_code);
                $wechat_user_msg->save();
            }
        }

        // 4.审核通过之后，生成后台参录账号
        $msg = "";
        if ($user_company_msg['company_status'] == 3) {
            $request = [
                'username'       => $user_company_msg['company_name'],
                'department_id'  => 1,
                'email'          => $wechat_user_msg['phone'],
                'jobs'           => [2],
                'password'       => $wechat_user_msg['phone'],
                'roles'          => [10],
                'id'             => 0,
                'wechat_user_id' => $wechat_user_msg['id'],
            ];

            $user_app = (new Users());
            $user_app->storeBy($request);
            $user_app->attachRoles($request['roles']);
            $user_app->attachJobs($request['jobs']);

            $msg = " 后台网址 " . request()->domain();
            $msg .= "，账号：" . $request['email'];
            $msg .= "，密码：" . $request['password'];
        }

        // 5.发消息
        (new Notice())->saveDataCC([
            // 公司认证——审核结果
            'notice_type'    => 1,
            'title'          => $user_company_msg['company_status_name'],
            'content'        => $user_company_msg['company_status_note'] . $msg,
            'wechat_user_id' => $user_company_msg['wechat_user_id'],
        ]);

        $this->commit();

        return true;
    }
}
